package com.whfc.emp.config;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.mqtt.MqttProperties;
import com.whfc.common.spring.AppContextUtil;
import com.whfc.common.third.baidu.BaiDuFaceProperties;
import com.whfc.common.third.helmet.HelmetProperties;
import com.whfc.common.third.map.MapApi;
import com.whfc.common.third.map.impl.QQMapApi;
import com.whfc.common.third.map.impl.QQMapProperties;
import com.whfc.common.third.juhe.JuheFaceVerifyProperties;
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import com.baidu.aip.face.AipFace;

import java.util.concurrent.Executor;

/**
 * @Description: 人员模块配置文件导入
 * @author: xugcheng
 * @version: 1.0
 * @date: 2019/10/10 10:13
 */
@Configuration
@NacosPropertySources(value = {
        @NacosPropertySource(dataId = "com.whfc.emp.properties", autoRefreshed = true),
        @NacosPropertySource(dataId = "com.whfc.common.properties", autoRefreshed = true)
})
public class EmpConfig implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Value("${spring.profiles.active}")
    private String profile;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
    public AppContextUtil appContextUtil() {
        return new AppContextUtil();
    }

    @Bean
    public Executor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setThreadGroupName("emp-thg");
        taskExecutor.setThreadNamePrefix("emp-th");
        taskExecutor.setCorePoolSize(10);
        taskExecutor.setMaxPoolSize(50);
        taskExecutor.setQueueCapacity(1024);
        taskExecutor.setKeepAliveSeconds(30000);
        return taskExecutor;
    }

    @Bean("msFilePathConfig")
    public FilePathConfig msFilePathConfig() {
        FilePathConfig config = new FilePathConfig("ms", profile);
        return config;
    }

    /**
     * 腾讯地图API-参数
     *
     * @return
     */
    @Bean(name = "qqMapProps")
    @ConfigurationProperties(prefix = "qqmap")
    public QQMapProperties qqMapProps() {
        return new QQMapProperties();
    }

    /**
     * 腾讯地图API
     *
     * @param qqMapProps
     * @return
     */
    @Bean(name = "qqMapApi")
    public MapApi qqMapApi(@Qualifier("qqMapProps") QQMapProperties qqMapProps) {
        return new QQMapApi(qqMapProps.getKey(), qqMapProps.getSk());
    }

    @Bean(name = "mqttProperties")
    @ConfigurationProperties(prefix = "mqtt")
    public MqttProperties getMqttProperties() {
        return new MqttProperties();
    }

    /**
     * 百度人脸识别应用参数
     * @return
     */
    @Bean(name = "baiDuFaceProperties")
    @ConfigurationProperties(prefix = "baidu")
    public BaiDuFaceProperties baiDuProperties() {
        return new BaiDuFaceProperties();
    }

    /**
     * 百度人脸识别api
     * @param baiDuFaceProperties
     * @return
     */
    @Bean(name = "baiDuAiFace")
    public AipFace client(@Qualifier("baiDuFaceProperties") BaiDuFaceProperties baiDuFaceProperties){
        AipFace client = new AipFace(baiDuFaceProperties.getAppid(), baiDuFaceProperties.getAppkey(), baiDuFaceProperties.getSecretkey());
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        return client;
    }

    @Bean
    @ConfigurationProperties(prefix = "helmet.gps")
    public HelmetProperties helmetProps() {
        return new HelmetProperties();
    }

    /**
     * 聚合数据人脸对比认证配置
     * @return
     */
    @Bean
    @ConfigurationProperties(prefix = "juhe.faceverify")
    public JuheFaceVerifyProperties juheFaceVerifyProps() {
        return new JuheFaceVerifyProperties();
    }

}